home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / extensions / samples / mkCurRd.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  12KB  |  357 lines

  1. /*
  2.  * Copyright (c) 1994 Silicon Graphics, Inc.
  3.  * 
  4.  * Permission to use, copy, modify, distribute, and sell this software and
  5.  * its documentation for any purpose is hereby granted without fee,
  6.  * provided that (i) the above copyright notices and this permission
  7.  * notice appear in all copies of the software and related documentation,
  8.  * and (ii) the name of Silicon Graphics may not be used in any
  9.  * advertising or publicity relating to the software without the specific,
  10.  * prior written permission of Silicon Graphics.
  11.  * 
  12.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  13.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  14.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  15.  * 
  16.  * IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL,
  17.  * INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY
  18.  * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  19.  * OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  20.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  21.  * OF THIS SOFTWARE.
  22.  */
  23.  
  24. /*
  25.  *  
  26.  * cc -o mkCurRd mkCurRd.c -lGLU -lGL -lX11 -lm
  27.  *
  28.  * Usage:
  29.  *  mkCurRd [windId in hex] [-h/H for help]
  30.  *   With out argument creates two windows and reads from one into the other.
  31.  *   With argument windId uses windId as read window.
  32.  *  Keys:
  33.  *   a/A   - increase/reduce copy speed
  34.  *   c     - do one copy
  35.  *   C     - Continous copy
  36.  *   h/H/? - print key usage.
  37.  *   When ran without an argument the following are also relevant
  38.  *   f   - flip read/write windows
  39.  *   t   - change the intensity of the image in the read window
  40.  *   
  41.  *   
  42.  */
  43. #include <stdlib.h>
  44. #include <stdio.h>
  45. #include <string.h>
  46. #include <math.h>
  47. #include <GL/glx.h>
  48. #include <GL/glu.h>
  49. #include <X11/Xutil.h>
  50. #include <X11/keysym.h>
  51. #include "xwindow.h"
  52.  
  53. int widths[2], heights[2];
  54. float clearColors[2][3];
  55. Window wins[2];
  56. Display *dpy;
  57. float intensity = 1;
  58.  
  59. #ifndef GLX_SGI_make_current_read
  60. /* Make The compiler barf */
  61. GLX_SGI_make_current_read extension is not supported.
  62. #else
  63.  
  64. void
  65. changeIntensity(float delta)
  66. {
  67.     if (intensity <= delta)
  68.         intensity = 1;
  69.     else
  70.         intensity -= delta;
  71. }
  72.  
  73. void
  74. drawWindow(int windex)
  75. {
  76.     glClearColor(clearColors[windex][0],clearColors[windex][1],
  77.                  clearColors[windex][2], 1.0);
  78.     glClear(GL_COLOR_BUFFER_BIT);
  79.  
  80.     glViewport(0, 0, widths[windex], heights[windex]);
  81.     /* draw a polygon */
  82.     glBegin(GL_QUADS);
  83.     glColor3f(intensity, 0.0, 0.0);
  84.     glVertex2f(-0.8,-0.8);
  85.     glColor3f(0.0, intensity, 0.0);
  86.     glVertex2f( 0.8,-0.8);
  87.     glColor3f(0.0, 0.0, intensity);
  88.     glVertex2f( 0.8, 0.8);
  89.     glColor3f(intensity, intensity, 0.0);
  90.     glVertex2f(-0.8, 0.8);
  91.     glEnd();
  92.     glDisable(GL_TEXTURE_2D);
  93.  
  94.     /* draw an outline around the polygon */
  95.     glLineWidth(2.0);
  96.     glColor3f(1.0, 1.0, 1.0);
  97.     glBegin(GL_LINE_LOOP);
  98.     glVertex2f(-0.8,-0.8);
  99.     glVertex2f( 0.8,-0.8);
  100.     glVertex2f( 0.8, 0.8);
  101.     glVertex2f(-0.8, 0.8);
  102.     glEnd();
  103.  
  104.     glXSwapBuffers(dpy, wins[windex]);
  105. }
  106.  
  107. static void
  108. usage(char *name, char *extra, int status)
  109. {
  110.     if (extra != NULL)
  111.         fprintf(stderr, "%s: %s\n", name, extra);
  112.     fprintf(stderr, "usage: %s [read windId in hex] [-h/H (for help)]\n", name);
  113.     fprintf(stderr,
  114.             "\tInteractively, `h/?' keys provide description of interface\n");
  115.     exit(status);
  116. }
  117.  
  118. int
  119. main(int argc, char *argv[])
  120. {
  121.     int visualAttr[] = { GLX_RGBA, GLX_RED_SIZE, 1, GLX_DOUBLEBUFFER, None };
  122.     XEvent ev;
  123.     int drawWin, readWin;
  124.     GLXContext ctx;
  125.     int x = 0, y = 0, i;
  126.     int width = 300, height = 300;
  127.     int refreshWrite = True, refreshRead = True;
  128.     int copyWindow = False, continuous = False;
  129.     int myRead;
  130.     int xStart = 299, yStart = 299;
  131.     int speed = 1;
  132.     int zoom = 1;
  133.     const char *extensions;
  134.  
  135.     if (argc > 1 && argv[1][0] == '-' &&
  136.         (argv[1][1] == 'h' || argv[1][1] == 'H')) {
  137.         usage(argv[0], NULL, EXIT_SUCCESS);
  138.     }
  139.     clearColors[0][1] = 1;
  140.     clearColors[0][0] = clearColors[0][2] = 0;
  141.     clearColors[1][0] = 1;
  142.     clearColors[1][1] = clearColors[0][2] = 0;
  143.  
  144.     widths[0] = widths[1] = width;
  145.     heights[0] = heights[1] = height;
  146.     createWindowAndContext(&dpy, &wins[0], &ctx, x, y, width, height,
  147.                            GL_FALSE, NULL, visualAttr, "MakeCurrentRead0");
  148.     if (argc > 1) {
  149.         wins[1] = strtol(argv[1], NULL, 16);
  150.         printf("Read window 0x%x\n", wins[1]);
  151.     myRead = False;
  152.     } else {
  153.         XSetWindowAttributes swa;
  154.         XWindowAttributes gwa;
  155.  
  156.     myRead = True;
  157.         XGetWindowAttributes(dpy, wins[0], &gwa);
  158.         swa.background_pixel = None;
  159.         swa.border_pixel = None;
  160.         swa.colormap = gwa.colormap;
  161.         swa.event_mask = StructureNotifyMask | KeyPressMask | ExposureMask;
  162.         wins[1] = XCreateWindow(dpy, gwa.root,
  163.                                 x, y, width, height, 0,
  164.                                 gwa.depth, InputOutput, gwa.visual,
  165.                                 CWBackPixel | CWBorderPixel | CWColormap |
  166.                                 CWEventMask, &swa);
  167.     XStoreName(dpy, wins[1], "MakeCurrentRead1");
  168.         mapWindowAndWait(dpy, wins[1]);
  169.     }
  170.     /* Verify make_current_read extension support */
  171.     extensions = glXQueryExtensionsString(dpy, DefaultScreen(dpy));
  172.     if (extensions == NULL ||
  173.         strstr(extensions, "GLX_SGI_make_current_read") == NULL) {
  174.         fprintf(stderr, "The make_current_read extension\
  175.  is not supported on this system\n");
  176.         exit(EXIT_FAILURE);
  177.     }
  178.     /*
  179.      * Clear the window(s).
  180.      */
  181.     for (i = (argc > 1) ? 0 : 1; i > -1; i--) {
  182.         int j;
  183.         for (j = 0; j < 2; j++) {
  184.             glXMakeCurrent(dpy, wins[i], ctx);
  185.             glClearColor(clearColors[i][0],clearColors[i][1],
  186.                          clearColors[i][2], 1.0);
  187.             glClear(GL_COLOR_BUFFER_BIT);
  188.             glXSwapBuffers(dpy, wins[i]);
  189.         }
  190.     }
  191.     glXMakeCurrentReadSGI(dpy, wins[0], wins[1], ctx);
  192.     glReadBuffer(GL_FRONT);
  193.     drawWin = 0;
  194.     readWin = 1;
  195.  
  196.     while (1) {
  197.     while (XPending(dpy)) {
  198.         XNextEvent(dpy, &ev);
  199.         switch (ev.type) {
  200.           case KeyPress:
  201.               {
  202.                   KeySym ks;
  203.  
  204.                   XLookupString(&ev.xkey, NULL, 0, &ks, NULL);
  205.                   switch (ks) {
  206.                     case XK_Escape:
  207.                       exit(EXIT_SUCCESS);
  208.                       break;
  209.                     case XK_a:
  210.                       if (speed < 50)
  211.                           speed++;
  212.                       else
  213.                           fprintf(stderr, "Maximum speed reached \07\n");
  214.                       break;
  215.                     case XK_A:
  216.                       if (speed > 1)
  217.                           speed--;
  218.                       break;
  219.                     case XK_f:
  220.                       if (argc < 2) {
  221.                           drawWin = (drawWin + 1) & 0x1;
  222.                           readWin = (drawWin + 1) & 0x1;
  223.                           glXMakeCurrentReadSGI(dpy, wins[drawWin],
  224.                                                 wins[readWin], ctx);
  225.                           xStart = widths[readWin] - 1;
  226.                           yStart = heights[readWin] - 1;
  227.                           refreshWrite = !continuous;
  228.                       } else {
  229.                           fprintf(stderr,
  230.                                   "Flip not allowed for foreign windows\n");
  231.                       }
  232.                       break;
  233.                     case XK_c:
  234.                       if (xStart <= speed)
  235.                           xStart = widths[readWin] - 1;
  236.                       else
  237.                           xStart -= speed;
  238.                       if (yStart <= speed)
  239.                           yStart = heights[readWin] - 1;
  240.                       else
  241.                           yStart -= speed;
  242.                       copyWindow = True;
  243.                       break;
  244.                     case XK_C:
  245.                       continuous = !continuous;
  246.                       break;
  247.                     case XK_z:
  248.                       if (zoom < 4) {
  249.                           zoom += 1;
  250.                           glPixelZoom(zoom, zoom);
  251.                       }
  252.                       break;
  253.                     case XK_Z:
  254.                       if (zoom > 1) {
  255.                           zoom -= 1;
  256.                           glPixelZoom(zoom, zoom);
  257.                       }
  258.                       break;
  259.                     case XK_t:
  260.                       changeIntensity(0.1);
  261.                       refreshRead = True;
  262.                       break;
  263.                     case XK_question:
  264.                     case XK_h:
  265.                     case XK_H:
  266.                       printf("\nKeys:\n\
  267.     a/A   - increase/reduce copy speed\n\
  268.     c     - do one copy\n\
  269.     C     - toggle continous copy\n\
  270.     z/Z   - increase/reduce pixel zoom\n\
  271.     h/H/? - print key usage.\n\
  272.   When ran without an argument the following are also relevant\n\
  273.     f   - flip read/write windows\n\
  274.     t   - change the intensity of the image in the read window\n");
  275.                       break;
  276.                     default:
  277.                       break;
  278.                   }
  279.               }
  280.                       break;
  281.           case ConfigureNotify:
  282.                 width = ev.xconfigure.width;
  283.                 height = ev.xconfigure.height;
  284.         if (ev.xconfigure.window == wins[drawWin]) {
  285.                     widths[drawWin] = width;
  286.                     heights[drawWin] = height;
  287.                     refreshWrite = !continuous;
  288.         } else {
  289.                     widths[readWin] = width;
  290.                     heights[readWin] = height;
  291.                     refreshRead = True;
  292.                 }
  293.         break;
  294.           case Expose:
  295.         if (ev.xexpose.window == wins[drawWin])
  296.                     refreshWrite = !continuous;
  297.                 else
  298.                     refreshRead = True;
  299.         break;
  300.           default:
  301.         break;
  302.         }
  303.     }
  304.  
  305.         if (myRead && refreshRead) {
  306.             glXMakeCurrent(dpy, wins[readWin], ctx);
  307.             drawWindow(readWin);
  308.             refreshRead = False;
  309.             glXMakeCurrentReadSGI(dpy, wins[drawWin],
  310.                                   wins[readWin], ctx);
  311.         }
  312.         if (continuous) {
  313.             if (xStart <= speed)
  314.                 xStart = widths[readWin] - 1;
  315.             else
  316.                 xStart -= speed;
  317.             if (yStart <= speed)
  318.                 yStart = heights[readWin] - 1;
  319.             else
  320.                 yStart -= speed;
  321.             copyWindow = True;
  322.         }
  323.     if (copyWindow) {
  324.             int w = widths[readWin] - xStart;
  325.             int h = heights[readWin] - yStart;
  326.  
  327.             if (w < 0)
  328.                 w = widths[readWin];
  329.             if (h < 0)
  330.                 h = heights[readWin];
  331.             glClearColor(clearColors[drawWin][0],clearColors[drawWin][1],
  332.                          clearColors[drawWin][2], 1.0);
  333.         glClear(GL_COLOR_BUFFER_BIT);
  334.             glRasterPos2i(-1,-1);
  335.             glCopyPixels(xStart, yStart, widths[readWin] - xStart,
  336.                          heights[readWin] - yStart, GL_COLOR);
  337.             copyWindow = False;
  338.             glXSwapBuffers(dpy, wins[drawWin]);
  339.         } else if (refreshWrite) {
  340.             drawWindow(drawWin);
  341.         refreshWrite = False;
  342.     }
  343. #if 0
  344.         /* For debugging */
  345.         {
  346.             GLenum error;
  347.             while ((error = glGetError()) != GL_NO_ERROR) {
  348.                 fprintf(stderr, "Error: %s\n",
  349.                         (char *) gluErrorString(error));
  350.             }
  351.         }
  352. #endif
  353.  
  354.     }
  355. }
  356. #endif /* Make_current_read extension is not defined (else part) */
  357.